package com.oa.admin.service.impl;

import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.BusinessCardApplyAddDTO;
import com.oa.admin.dto.BusinessCardApplyListDTO;
import com.oa.admin.vo.BusinessCardApplyDetailsVO;
import com.oa.admin.vo.BusinessCardApplyListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.BusinessCardApply;
import com.oa.admin.mapper.BusinessCardApplyMapper;
import com.oa.admin.service.IBusinessCardApplyService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.entity.User;
import com.oa.system.service.IApplyApprovalService;
import com.oa.system.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 名片申请表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class BusinessCardApplyServiceImpl extends ServiceImpl<BusinessCardApplyMapper, BusinessCardApply> implements IBusinessCardApplyService {

    private final BusinessCardApplyMapper businessCardApplyMapper;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    private IUserService userService;

    @Autowired
    public BusinessCardApplyServiceImpl(BusinessCardApplyMapper businessCardApplyMapper) {
        this.businessCardApplyMapper = businessCardApplyMapper;
    }

    @Override
    @CommonService
    public PageResult<BusinessCardApply> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<BusinessCardApply> list = businessCardApplyMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<BusinessCardApply> page = new Page<>(pageNum, pageSize);
            //查询
            List<BusinessCardApply> list = businessCardApplyMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public BusinessCardApplyDetailsVO findBusinessCardApplyById(String id){
        return businessCardApplyMapper.findBusinessCardApplyDetails(id);
    }

    @CommonService
    @Override
    public PageResult<BusinessCardApplyListVO> findBusinessCardApplyList(BusinessCardApplyListDTO businessCardApplyListDTO) {
        Page page = businessCardApplyListDTO.getPage();
        return new PageResult<>(businessCardApplyMapper.findBusinessCardApplyList(page, businessCardApplyListDTO), page);
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp addBusinessCardApply(BusinessCardApplyAddDTO businessCardApplyAddDTO) throws DataIsEmptyException, BusinessException {
        BusinessCardApply businessCardApply = new BusinessCardApply();
        BeanUtils.copyProperties(businessCardApplyAddDTO, businessCardApply);
        businessCardApply.setCreateTime(new Date());
        if(insert(businessCardApply)){
            User user = userService.selectById(businessCardApplyAddDTO.getProposer());

            ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
            applyApprovalAddDTO.setApplyId(businessCardApply.getId());
            applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.BUSINESS_CARD.getId());
            applyApprovalAddDTO.setApplyApprovalName(user.getName() + ProcessDefineEnum.BUSINESS_CARD.getText());
            applyApprovalAddDTO.setApprovalUserId(user.getId());

            return applyApprovalService.addApproval(applyApprovalAddDTO);
        }
        throw new DataIsEmptyException("提交申请失败");
    }

    @Override
    public void export(BusinessCardApplyListDTO businessCardApplyListDTO, HttpServletResponse response) {
        List<BusinessCardApplyListVO> businessCardApplyListVOS;
        if(businessCardApplyListDTO.getPage() != null){
            Page page = businessCardApplyListDTO.getPage();
            businessCardApplyListVOS = businessCardApplyMapper.findBusinessCardApplyList(page, businessCardApplyListDTO);
        }else{
            businessCardApplyListVOS = businessCardApplyMapper.findBusinessCardApplyList(new Page(), businessCardApplyListDTO);
        }

        String[] rows = {"id", "部门名称", "用户姓名", "数量", "提交时间", "使用时间", "备注", "印名片者", "审核状态", "备注"};
        try {
            ExcelUtil.downloadExcel("名片审批列表" + DateUtil.getDayMonth(), rows, businessCardApplyListVOS, "名片审批列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
